package com.slz3.business.module.wmsManagement.wmsTraysManage.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.goodsManagement.gooTypeLevel1.domain.GooTypeLevel1;
import com.slz3.business.module.goodsManagement.gooTypeLevel2.domain.GooTypeLevel2;
import com.slz3.business.module.wmsManagement.wmsTraysManage.domain.WmsTraysManage;
import com.slz3.business.module.wmsManagement.wmsTraysManage.domain.WmsTraysManageFactory;
import com.slz3.business.module.wmsManagement.wmsTraysManage.steward.WmsTraysManageSpecification;
import com.slz3.business.module.wmsManagement.wmsWarehouseManage.domain.WmsWarehouseManage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WmsTraysManageServiceImpl implements WmsTraysManageService {
    @Override
    public WmsTraysManage save(WmsTraysManage wmsTraysManage) {
        return Repositorys.wmsTraysManage.save(WmsTraysManageFactory.initBaseFields(wmsTraysManage));
    }

    @Override
    public Page<WmsTraysManage> page(MultiValueMap<String, String> params) {
        Page<WmsTraysManage> wmsTraysManagePage = Repositorys.wmsTraysManage.findAll(
                WmsTraysManageSpecification.findJoin(params), PageableSteward.extract(params)
        );

        if(wmsTraysManagePage.hasContent()){
            List<UUID> houseUuids = new ArrayList<>(wmsTraysManagePage.getSize());
            List<UUID> houseAreaUuids = new ArrayList<>(wmsTraysManagePage.getSize());
            List<UUID> level1Uuids = new ArrayList<>(wmsTraysManagePage.getSize());
            List<UUID> level2Uuids = new ArrayList<>(wmsTraysManagePage.getSize());

            wmsTraysManagePage.forEach(item -> {
                UUID wmsHouseUuid = item.getFkWmsHouseUuid();
                UUID wmsAreaUuid = item.getFkWmsAreaUuid();
                UUID level1Uuid = item.getFkGooTypeLevel1Uuid();
                UUID level2Uuid = item.getFkGooTypeLevel2Uuid();
                houseUuids.add(wmsHouseUuid);
                houseAreaUuids.add(wmsAreaUuid);
                level1Uuids.add(level1Uuid);
                level2Uuids.add(level2Uuid);
            });

            Map<Object, WmsWarehouseManage> houseMap = Repositorys.wmsWarehouseManage.findAllById(houseUuids)
                    .stream().collect(Collectors.toMap(WmsWarehouseManage::getUuid, Function.identity()));
            ;
            Map<Object, WmsWarehouseManage> areaMap = Repositorys.wmsWarehouseManage.findAllById(houseAreaUuids)
                    .stream().collect(Collectors.toMap(WmsWarehouseManage::getUuid, Function.identity()));
            Map<Object, GooTypeLevel1> level1Map = Repositorys.gooTypeLevel1.findAllById(level1Uuids)
                    .stream().collect(Collectors.toMap(GooTypeLevel1::getUuid, Function.identity()));
            Map<Object, GooTypeLevel2> level2Map = Repositorys.gooTypeLevel2.findAllById(level2Uuids)
                    .stream().collect(Collectors.toMap(GooTypeLevel2::getUuid, Function.identity()));

            wmsTraysManagePage.forEach(item -> {
                UUID wmsHouseUuid = item.getFkWmsHouseUuid();
                UUID wmsAreaUuid = item.getFkWmsAreaUuid();
                UUID level1Uuid = item.getFkGooTypeLevel1Uuid();
                UUID level2Uuid = item.getFkGooTypeLevel2Uuid();
                //查询管理的仓库和库区一级所属类别
                //1、仓库
                WmsWarehouseManage house = houseMap.get(wmsHouseUuid);
                item.setWmsWarehouse(house);
                //2、库区
                WmsWarehouseManage area = areaMap.get(wmsAreaUuid);
                item.setWmsWareArea(area);
                //商品一级类别
                GooTypeLevel1 typeLevel1 = level1Map.get(level1Uuid);
                item.setGooTypeLevel1(typeLevel1);
                //商品二级类别
                GooTypeLevel2 typeLevel2 = level2Map.get(level2Uuid);
                item.setGooTypeLevel2(typeLevel2);
            });
        }

        return wmsTraysManagePage;
    }


    @Override
    public WmsTraysManage modify(WmsTraysManage wmsTraysManageNew) {
        return Repositorys.wmsTraysManage.saveAndFlush(WmsTraysManageFactory.initBaseFields(wmsTraysManageNew));
    }

    @Override
    public Optional<WmsTraysManage> single(UUID uuid) {
        return Repositorys.wmsTraysManage.findById(uuid);
    }

    @Override
    public List<WmsTraysManage> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.wmsTraysManage.findAllById(uuids);
    }

    @Override
    public List<WmsTraysManage> delete(List<WmsTraysManage> wmsTraysManageList) {
        wmsTraysManageList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            WmsTraysManageFactory.initBaseFields(item);
        });
        return Services.wmsTraysManageTransactional.batchModify(wmsTraysManageList);
    }

}
